<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/4/17
 * Time: 15:41
 */
set_time_limit(0);
require_once('config.php');
require_once('mysqlii.php');
require_once('class.smtp.php');
require_once('class.phpmailer.php');


$db = MySqlii::getInstance();

$timeStart = strtotime(date('Y-m-d 00:00:00'));
$timeStop  = strtotime(date('Y-m-d 23:59:59'));

echo "<pre/>";

$bundle = getCukBundle();

foreach($bundle as $cukey => $values) {

    if(empty($cukey)) {
        continue;
    }

    if(empty($values)) {
        continue;
    }

    foreach($values as $val) {
        $allBUn[] = escape($val);
    }
    $allBundleStr = implode(',', $allBUn);
    $totlesql = "SELECT count(id) as num FROM reals_leshi WHERE bundleid IN ($allBundleStr)";
    $res = $db->fetch_row($db->query($totlesql));
    $totleNum = $res[0];

    $onesql = "SELECT count(id) as num FROM reals_leshi WHERE ctime BETWEEN $timeStart AND $timeStop AND bundleid IN ($allBundleStr)";
    $oneres = $db->fetch_row($db->query($onesql));
    $oneNum = $oneres[0];

    pushIiapple($cukey, $oneNum, $totleNum);

    /*
    if($totleNum >= 2000) {
        $sql = "SELECT count(id) as num, bundleid FROM reals_leshi WHERE ctime BETWEEN $timeStart AND $timeStop AND bundleid IN ($allBundleStr) GROUP BY bundleid";
        $result = $db->query($sql);
        $returnArr = array();
        while($access = $db->fetch_array($result)) {
            $returnArr[$access['bundleid']] = $access['num'];
        }
        checkPush($totleNum, $cukey, $returnArr);
    }*/

    unset($allBUn);
}

saveAllBundle();


function escape($str)
{
    return "'". $str."'";
}
/**
 * 获取联运游戏的bundleid
 * @author Giles
 * @return array
 */
function getCukBundle()
{
    $url = "http://www.iiapple.com/Interface/getCertMd5List";
    $data = json_decode(file_get_contents($url), true);
    $bundles = array();
    $cuks    = array();
    $lastBun = array();
    foreach($data['data'] as $key => $val) {
        $cuks[] = $key;
        foreach($val as $pkg_id) {
            $bundles[$key][] = $pkg_id['pkg_id'];
        }
    }
    foreach($cuks as $cuk) {
        if(!empty($bundles[$cuk])) {
            $lastBun[$cuk] = array_unique($bundles[$cuk]);
        }
    }

   return $lastBun;
}

function checkPush($totleNum, $type, $buarr = array()) {
    if($totleNum >= 2000 && $totleNum < 3000) {
        pushMail($totleNum, '2k3k', 2000, $type, $buarr);
    }

    if($totleNum >= 3000 && $totleNum < 4000) {
        pushMail($totleNum, '3k4k', 3000, $type, $buarr);
    }

    if($totleNum >= 4000 && $totleNum < 5000) {
        pushMail($totleNum, '4k5k', 4000, $type, $buarr);
    }

    if($totleNum >= 5000 && $totleNum < 6000) {
        pushMail($totleNum, '5k6k', 5000, $type, $buarr);
    }

    if($totleNum >= 6000 && $totleNum < 7000) {
        pushMail($totleNum, '6k7k', 6000, $type, $buarr);
    }

    if($totleNum >= 7000 && $totleNum < 8000) {
        pushMail($totleNum, '7k8k', 7000, $type, $buarr);
    }

    if($totleNum >= 8000 && $totleNum < 9000) {
        pushMail($totleNum, '8k9k', 8000, $type, $buarr);
    }

    if($totleNum >= 9000 && $totleNum < 10000) {
        pushMail($totleNum, '9k0k', 9000, $type, $buarr);
    }

    if($totleNum >= 10000) {
        pushMail($totleNum, '0k0k', 10000, $type, $buarr);
    }
}


function pushMail($num, $key, $val, $type, $buarr)
{
    $db = MySqlii::getInstance();
    $date = date('Ymd');
    $key = $key ."-". $type;

    $sql = "SELECT value FROM reals_leshi_mail WHERE name = '{$key}' AND date = $date";
    $res = $db->fetch_row($db->query($sql));
    if(empty($res)) {
        $inDsql = "INSERT INTO reals_leshi_mail (name, value, date) VALUES ('{$key}', 1, $date)";
        $db->query($inDsql);
    } else if($res[0] < 2) {
        $upSql = "UPDATE reals_leshi_mail SET value = value+1 WHERE name = '{$key}' AND date = $date";
        $db->query($upSql);
    } else {
        return;
    }

    $name = $type;
    $strings = '';
    foreach($buarr as $kkk => $vvv) {
        $strings .= $kkk .":". $vvv ."\n";
    }


    $contents = "警告!!!!<br/>
    $name 下载量为$num,超过临界值 ： $val 请悉知";
    $contents .=  "其中各个BundleId下载详情\n".$strings."<br/>";
    $sendUsers = array('308736929@qq.com', 'lijia@iiapple.com', '530972476@qq.com', '656297731@qq.com');
    //$sendUsers = array('656297731@qq.com');
    foreach($sendUsers as $userName){
        sendMail($userName, '下载超量预警！', $contents);
    }

    //pushIiapple($type, $num);
}


/**
 * 通过接口发送数据给pc端
 * @param $pushData
 */
function pushIiapple($cert_md5, $num, $allNum)
{
    $totleUrl = "http://www.iiapple.com/Interface/setCertTotal?cert_md5=$cert_md5&total=".$allNum;
    file_get_contents($totleUrl);
    $dayUrl   = "http://www.iiapple.com/Interface/setCertDays?cert_md5=$cert_md5&days=".$num;
    file_get_contents($dayUrl);

}

/**
 * 静态化bundleId
 */
function saveAllBundle()
{
    $url = "http://www.iiapple.com/Interface/getCertMd5List";
    $data = json_decode(file_get_contents($url), true);

    $bundles = array();

    foreach($data['data'] as $key => $val) {
        foreach($val as $bundle) {
            $bundles[] = $bundle['pkg_id'];
        }
    }
    $bundles = array_unique($bundles);

    $bundleString = '';
    foreach($bundles as $bundleK => $bundleV) {
        $bundleString.= $bundleK .' => "'. $bundleV."\",\n";
        $lastKey = $bundleK + 1;
    }
    file_put_contents('bundle.php', "<?php \n  \$bundleArr =  array(\n". $bundleString. "$lastKey => \"com.baidu.BaiduMobile\"\n); \n");
}



function sendMail ($mailto, $subject,  $content) {
    $mail = new PHPMailer(); //ʵ��һ��phpmailer����
    $mail->CharSet = "UTF-8";//ָ������
    $mail->Encoding = "base64";
    $mail->Port = '25';
    $mail->IsSMTP();
    $mail->Host = '124.202.152.216';
    $mail->SMTPAuth = true;
    $mail->Username = 'cs@ucenter.iiapple.com';
    $mail->Password = 'kXAkCzpws2XcpWYX';
    $mail->From = 'cs@ucenter.iiapple.com';
    $mail->FromName = 'I苹果用户中心';
    $mail->AddAddress($mailto);
    $mail->WordWrap = 500;
    $mail->IsHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $content;
    //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    $mail->Send();
}
